package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component  //只有在首次启动项目服务的时候才把这个注解放开, 访问数据库把数据放进redis里面, 此后重启服务不需要
public class StatsDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Value("${data.init}")
    private boolean init; //用配置的方式我们就不用经常动 @Component注解
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelStatsCacheService travelStatsCacheService;

    //Spring容器创建Bean完成之后, 触发监听器, 执行这个方法, 完成Redis数据的初始化
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (init) {
            // 1.从mysql里面拿到所有的文章
            List<StrategyDetail> list = strategyDetailService.list();
            // 2.循环迭代集合, 把文章数据封装vo对象里面
            for (StrategyDetail detail : list) {

                // 避免redis性能浪费, 判断了缓存里面没有该对象的key才缓存进去
                if (!strategyStatsCacheService.hasStatsKey(detail.getId())) {
                    StrategyStats vo = new StrategyStats();

                    vo.setThumbsupnum(detail.getThumbsupnum());
                    vo.setFavornum(detail.getFavornum());
                    vo.setReplynum(detail.getReplynum());
                    vo.setViewnum(detail.getViewnum());
                    vo.setStrategyId(detail.getId());
                    vo.setSharenum(detail.getSharenum());

                    vo.setDestId(detail.getDest().getId());
                    vo.setDestName(detail.getDest().getName());
                    vo.setTitle(detail.getTitle());

                    // 3.把vo存入redis缓存里面
                    strategyStatsCacheService.setStrategyStats(vo);
                }
                // 1.从mysql里面拿到所有的文章
                List<Travel> travels = travelService.list();
                // 2.循环迭代集合, 把文章数据封装vo对象里面
                for (Travel travel : travels) {

                    // 避免redis性能浪费, 判断了缓存里面没有该对象的key才缓存进去
                    if (!travelStatsCacheService.hasStatsKey(travel.getId())) {
                        TravelStats vo = new TravelStats();

                        vo.setThumbsupnum(travel.getThumbsupnum());
                        vo.setFavornum(travel.getFavornum());
                        vo.setReplynum(travel.getReplynum());
                        vo.setViewnum(travel.getViewnum());
                        vo.setTravelId(travel.getId());
                        vo.setSharenum(travel.getSharenum());


                        // 3.把vo存入redis缓存里面
                        travelStatsCacheService.setTravelStats(vo);
                    }
                }

                    // 把飙升排行榜数据存到redis的zset类型里面
                    RedisKey key = RedisKey.STRATEGY_STATS_UP_SORT;
                    Long strategyId = detail.getId();
                    if (!strategyStatsCacheService.exist(key, strategyId)) {
                        int num = detail.getViewnum() + detail.getReplynum();
                        strategyStatsCacheService.setCdsDatasAsZset(key, num, strategyId);
                    }

                    //把热门排行榜数据存到zset里面
                    RedisKey hotKey = RedisKey.STRATEGY_STATS_HOT_SORT;
                    if (!strategyStatsCacheService.exist(hotKey, strategyId)) {
                        int num = detail.getFavornum() + detail.getThumbsupnum();
                        strategyStatsCacheService.setCdsDatasAsZset(hotKey, num, strategyId);
                    }
                }
            }
        }
    }
